import atexit
import threading
import tkinter as tk

import database
from bot import WeChatBot
from Bot_UI import BotUI
from database import DatabaseManager, Session, engine



def main():
    # 创建微信机器人实例
    bot = WeChatBot()

    # 启动消息监听线程
    listen_thread = threading.Thread(target=bot.message_listener)
    listen_thread.daemon = True
    listen_thread.start()

    # 创建主窗口
    root = tk.Tk()

    def on_close():
        database.cleanup()
        root.destroy()  # 销毁窗口

    root.protocol('WM_DELETE_WINDOW', on_close)

    # 创建并初始化界面
    ui = BotUI(root, bot)




    # 进入主循环
    root.mainloop()


if __name__ == "__main__":
    main()